package cn.yyvtc.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author 黄后林
 * @date 2022-11-01 10:01:52
 * @note 程序员是用代码喂出来的
 */
public class MyBatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception{
        // 告诉Spring容器 xml文件在哪里?
        SqlSessionFactoryBean sqlSessionFactoryBean= new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:dao/*Dao.xml");
        sqlSessionFactoryBean.setMapperLocations(resources);
        return  sqlSessionFactoryBean;
    }


    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() throws Exception{
        // 告诉Spring容器  dao接口所在的包
        MapperScannerConfigurer mapperScannerConfigurer= new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("cn.yyvtc.workorderms.*.dao");
        //cn.yyvtc.workorderms.project.dao
        //cn.yyvtc.workorderms.workorder.dao

        //cn.yyvtc.workorderms.project.service.ProjectService
        return mapperScannerConfigurer;
    }
}
